package com.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.tm.annotation.GlobalTransaction;
import com.tm.transactional.GlobalTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author laowu
 * @date 2020/9/22 16:27
 * @desc
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${request-url}")
    private String requestUrl;
    @PostMapping
    @GlobalTransaction
    @Transactional
    public void save() {
        jdbcTemplate.execute("insert into t_goods values(1, 'iphone')");
        HttpHeaders headers = new HttpHeaders();
        headers.set("xid", GlobalTransactionManager.getCurrentXid());
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(headers);
        restTemplate.postForObject(requestUrl, httpEntity, String.class);
        int i = 100 / 0;
    }

}
